package webServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
*
*
* @Auther jiajin
*/

public class SocketDemo {
    public static void main(String[] args) {
        try (Socket socket = new Socket("127.0.0.1", 8080);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

            // 发送 HTTP 请求到 Web 服务器
            out.println("GET /servlet/servlet.ServletDemo HTTP/1.1");
            out.println("Host: localhost:8080");
            out.println("Connection: Close");
            out.println();
            
            System.out.println("开始读取响应");
            // 读取响应
            StringBuffer sb = new StringBuffer(8096);
            String line;
            
            while ((line = in.readLine()) != null) {
                sb.append(line).append("\n");
            }

            // 在控制台输出响应
            System.out.println(sb.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        	System.out.println("out");
        }
    }
}
